################################################################################
# CLI - A simple command line interface.
# Copyright (C) 2016-2021 Daniele Pallastrelli
#
# Boost Software License - Version 1.0 - August 17th, 2003
#
# Permission is hereby granted, free of charge, to any person or organization
# obtaining a copy of the software and accompanying documentation covered by
# this license (the "Software") to use, reproduce, display, distribute,
# execute, and transmit the Software, and to prepare derivative works of the
# Software, and to permit third-parties to whom the Software is furnished to
# do so, all subject to the following:
#
# The copyright notices in the Software and this entire statement, including
# the above license grant, this restriction and the following disclaimer,
# must be included in all copies of the Software, in whole or in part, and
# all derivative works of the Software, unless such copies or derivative
# works are solely in the form of machine-executable object code generated by
# a source language processor.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
################################################################################

cmake_minimum_required(VERSION 3.12)

project(
    cli
    VERSION 2.2.0
    # DESCRIPTION "A library for interactive command line interfaces in modern C++"
    LANGUAGES CXX
)

include(GNUInstallDirs)

option(CLI_BuildExamples "Build the examples." OFF)
option(CLI_BuildTests "Build the unit tests." OFF)
option(CLI_UseBoostAsio "Use the boost asio library." OFF)
option(CLI_UseStandaloneAsio "Use the standalone asio library." OFF)


if(WIN32)
    macro(get_WIN32_WINNT version)
        if(CMAKE_SYSTEM_VERSION)
            set(ver ${CMAKE_SYSTEM_VERSION})
            string(REGEX MATCH "^([0-9]+).([0-9])" ver ${ver})
            string(REGEX MATCH "^([0-9]+)" verMajor ${ver})
            # Check for Windows 10, b/c we'll need to convert to hex 'A'.
            if("${verMajor}" MATCHES "10")
                set(verMajor "A")
                string(REGEX REPLACE "^([0-9]+)" ${verMajor} ver ${ver})
            endif()
            # Remove all remaining '.' characters.
            string(REPLACE "." "" ver ${ver})
            # Prepend each digit with a zero.
            string(REGEX REPLACE "([0-9A-Z])" "0\\1" ver ${ver})
            set(${version} "0x${ver}")
        endif()
    endmacro()

    get_WIN32_WINNT(ver)
    # add_definitions(-D_WIN32_WINNT=${ver})
endif()

find_package(Threads REQUIRED)

# ----------------------------
# Boost
# ----------------------------
if(CLI_UseBoostAsio)
    # Create a dummy INTERFACE library to carry Boost headers
    add_library(BoostSystem INTERFACE)
    target_compile_definitions(BoostSystem INTERFACE
        BOOST_ERROR_CODE_HEADER_ONLY
        BOOST_SYSTEM_NO_LIB
    )

    find_package(Boost REQUIRED) # without COMPONENTS, header-only usage
    target_include_directories(BoostSystem SYSTEM INTERFACE ${Boost_INCLUDE_DIRS})

    install(TARGETS BoostSystem EXPORT cliTargets)
endif()

# ----------------------------
# Standalone Asio
# ----------------------------
if(CLI_UseStandaloneAsio)
    find_path(STANDALONE_ASIO_INCLUDE_PATH NAMES "asio.hpp" HINTS ${ASIO_INCLUDEDIR})
    mark_as_advanced(STANDALONE_ASIO_INCLUDE_PATH)
endif()

# ----------------------------
# Main CLI interface library
# ----------------------------
add_library(cli INTERFACE)
add_library(cli::cli ALIAS cli)

target_include_directories(cli
    INTERFACE
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

target_link_libraries(cli INTERFACE Threads::Threads)

if(CLI_UseBoostAsio)
    target_link_libraries(cli INTERFACE BoostSystem)
    target_compile_definitions(cli INTERFACE BOOST_ASIO_NO_DEPRECATED)
endif()

if(CLI_UseStandaloneAsio)
    add_library(standalone_asio INTERFACE IMPORTED)
    target_include_directories(standalone_asio SYSTEM INTERFACE ${STANDALONE_ASIO_INCLUDE_PATH})
    target_link_libraries(standalone_asio INTERFACE Threads::Threads)
    target_link_libraries(cli INTERFACE standalone_asio)
endif()

if(NOT DEFINED CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 14)
endif()
target_compile_features(cli INTERFACE cxx_std_${CMAKE_CXX_STANDARD})

# ----------------------------
# Examples
# ----------------------------
if(CLI_BuildExamples)
    add_subdirectory(examples)
endif()

# ----------------------------
# Tests
# ----------------------------
if(CLI_BuildTests)
    enable_testing()
    add_subdirectory(test)
endif()

# ----------------------------
# Install
# ----------------------------
if(NOT CMAKE_SKIP_INSTALL_RULES)
    install(DIRECTORY include/cli DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

    include(CMakePackageConfigHelpers)
    configure_package_config_file(
        "cliConfig.cmake.in"
        "${CMAKE_CURRENT_BINARY_DIR}/cliConfig.cmake"
        INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cli"
    )

    # Generate pkg-config .pc file
    set(PKGCONFIG_INSTALL_DIR
        ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig
        CACHE PATH "Installation directory for pkg-config (cli.pc) file"
    )
    configure_file("cli.pc.in" "cli.pc" @ONLY)

    install(TARGETS cli EXPORT cliTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
    install(EXPORT cliTargets FILE cliTargets.cmake NAMESPACE cli:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cli)

    install(
        FILES "${CMAKE_CURRENT_BINARY_DIR}/cliConfig.cmake"
        DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cli"
    )
    install(
        FILES "${CMAKE_CURRENT_BINARY_DIR}/cli.pc"
        DESTINATION ${PKGCONFIG_INSTALL_DIR}
    )
endif()
